package com.dofast.bt.mvp.presenter.basket;

import com.dofast.bt.R;
import com.dofast.bt.adapter.BasketAdapter;
import com.dofast.bt.adapter.SelectCallBack;
import com.dofast.bt.base.BaseMvpPresenter;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.bean.ShoppingCartBean;
import com.dofast.bt.mvp.model.IMainModel;
import com.dofast.bt.mvp.model.MainModel;
import com.dofast.bt.mvp.view.activity.basket.BuyListView;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * username:Created by hsj on 2016/12/25 0025.
 * class：
 * 类描述：购买列表界面
 */

public class BuyListPresenter extends BaseMvpPresenter<BuyListView> implements IBuyListPresenter {

    private BasketAdapter adapter;
    private List<ShoppingCartBean> list;
    private IMainModel mainModel;

    public BuyListPresenter() {
        mainModel = new MainModel();
    }

    @Override
    public void initData() {
        checkViewAttach();
        list = new ArrayList<ShoppingCartBean>();
        adapter = new BasketAdapter(list, new SelectCallBack() {
            @Override
            public void select(int position, boolean flags) {
                //// TODO: 2016/12/9 0009 商品选择逻辑处理

            }
        });
        mvpView.initAdapter(adapter);
        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        try {
            list.clear();
            list.addAll(mainModel.getShoppingCar());
            adapter.notifyDataSetChanged();
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void itemClick(int position) {
        if (list.get(position).isCheck()) {
            list.get(position).setCheck(false);
        } else {
            list.get(position).setCheck(true);
        }
        adapter.notifyDataSetChanged();
    }

    private boolean isCheck = true;

    @Override
    public void selectAll() {
        for (ShoppingCartBean sb : list
                ) {
            sb.setCheck(isCheck);
        }
        if (isCheck) {
            isCheck = false;
            mvpView.showSelectAll();
        } else {
            isCheck = true;
            mvpView.hideSelectAll();
        }

       /* for (GoodsBean good : list) {
            good.setCheck(isCheck);
        }*/
        adapter.notifyDataSetChanged();
    }

    @Override
    public void submit() {
        if (isEdit) {
            //结算
            List<ShoppingCartBean> goodsList = new ArrayList<ShoppingCartBean>();
            for (ShoppingCartBean shoppingCartBean : list) {
                if (shoppingCartBean.isCheck()) {
                    goodsList.add(shoppingCartBean);
                }
            }

            mvpView.gotoSubmitOrderActivity(goodsList);
        } else {
            //删除
            delete();
        }

    }

    private boolean isEdit = true;

    public void edit() {
        if (isEdit) {
            isEdit = false;
            mvpView.setEdit("完成");
            mvpView.setSubmit("删除");

        } else {
            isEdit = true;
            mvpView.setEdit("编辑");
            mvpView.setSubmit("结算");

        }
    }


    /**
     * 删除逻辑
     */
    private void delete() {
        try {
            for (ShoppingCartBean sc : getGoods()
                    ) {
                mainModel.deleteCar(sc.getGoodsId());
            }
            getData();
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    /**
     * @return 选中的商品
     */
    private List<ShoppingCartBean> getGoods() throws DbException {
        List<ShoppingCartBean> goods = new ArrayList<ShoppingCartBean>();

        for (ShoppingCartBean good : list) {
            if (good.isCheck()) {
                goods.add(good);
            }
        }
        return goods;
    }
}
